//    Copyright (C) Kherty.  All rights reserved.
namespace OpenLS.Spreadsheet
{
    class ColumnConverter
    {
        public static int ColumnNameToIndex(string s)
        {
            int result = 0;
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (!char.IsLetter(c))
                    return result;
                result = result * 26;
                result = result + (c - 'A');
            }
            return result;
        }

        public static string IndexToName(int Index)
        {
            if (Index == 0)
                return "A";
            string result = "";
            int i = Index;
            while (i >= 0)
            {
                int last = i % 26;
                char c = (char) ('A' + last);
                result = c + result;
                i = (i / 26)-1;
            }
            return result;
        }
    }
}
